#include "lcd.h"

void init_lcd(struct lcd_info *lcdinfo)
{
    lcdinfo->fd = open("/dev/fb0", O_RDWR);
    if(lcdinfo->fd == -1)
    {
        perror("打开LCD设备失败");
        exit(0);
    }

    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    // 获取LCD硬件参数
    ioctl(lcdinfo->fd, FBIOGET_VSCREENINFO, &vinfo);
    ioctl(lcdinfo->fd, FBIOGET_FSCREENINFO, &finfo);

    lcdinfo->width = vinfo.xres;    
    lcdinfo->height= vinfo.yres;    
    lcdinfo->bpp   = vinfo.bits_per_pixel;    
    int screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel/8;

    // 将映射内存设置为 800 * 480
    lcdinfo->fb_mem = mmap(NULL, screensize, PROT_READ|PROT_WRITE,
                           MAP_SHARED, lcdinfo->fd, 0);
    if(lcdinfo->fb_mem == MAP_FAILED)
    {
        perror("内存映射失败");
        exit(0);
    }
}

void clean_screen(struct lcd_info *lcdinfo)
{
    // 清屏，白屏
    int screensize = lcdinfo->width * lcdinfo->height * lcdinfo->bpp/8;
    memset(lcdinfo->fb_mem, 0xFFFFFFFF, screensize);
}

void release_lcd(struct lcd_info *lcdinfo)
{
    if(lcdinfo == NULL)
        return;
    
    munmap(lcdinfo->fb_mem, 800*480*4);
}